#include "../Renderer/Texture2D.h"

namespace Nitro::Renderer {

Texture2D CreateTexture2D(const TextureDescriptor2D &texDesc,
                          const SourceImageDescriptor &srcDesc)
{
    uint32_t texture;
    glCreateTextures(GL_TEXTURE_2D, 1, &texture);
    glTextureParameteri(texture, GL_TEXTURE_WRAP_S, texDesc.wrappingX);
    glTextureParameteri(texture, GL_TEXTURE_WRAP_T, texDesc.wrappingY);
    glTextureParameteri(texture, GL_TEXTURE_MAG_FILTER, texDesc.magFilter);
    glTextureParameteri(texture, GL_TEXTURE_MIN_FILTER, texDesc.minFilter);
    glTextureParameterf(texture, GL_TEXTURE_MAX_ANISOTROPY, 16.0f);
    switch (srcDesc.format)
    {
        case RGB8UInt:
            glTextureStorage2D(texture, 8, GL_RGB8, srcDesc.width,
                               srcDesc.height);
            glTextureSubImage2D(texture, 0, 0, 0, srcDesc.width, srcDesc.height,
                                GL_RGB, GL_UNSIGNED_BYTE, srcDesc.data);
            break;
        case RGBA8UInt:
            glTextureStorage2D(texture, 8, GL_RGBA8, srcDesc.width,
                               srcDesc.height);
            glTextureSubImage2D(texture, 0, 0, 0, srcDesc.width, srcDesc.height,
                                GL_RGBA, GL_UNSIGNED_BYTE, srcDesc.data);
            break;

        default: throw "Image format not yet supported\n";
    }
    return {texture};
}

void Texture2D::generateMipmap() { glGenerateTextureMipmap(id); }

}; // namespace Nitro::Renderer